package cn.zktr.Personnel.controllers.zhouyongcong.end;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.zktr.Personnel.model.pojos.PerPmsDept;
import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.service.zhouyongcong.findService;

@Controller
public class deptController {
	
	@Autowired 
	private findService fs;
	
	@RequestMapping("zyc/dept-det")
	public void dept_det(int id,Model mod){
		mod.addAttribute("dept",fs.findDeptid(id));
	}
	
	@RequestMapping("zyc/dept-table")
	public void depttable() {}
	
	@RequestMapping("zyc/dept-add")
	public void dept_add(Model mod) {
		List<PerPmsEmp> list2 = new ArrayList<>();
		for(PerPmsEmp ppe : fs.findAllEmp()) {
			if(ppe.getPerPmsStms().get(0).getPerPmsPot().getPerPmsZhiji().getZhijiid()==4) {
				list2.add(ppe);
			}
		}
		List<PerPmsDept> list1 = new ArrayList<>();
		for(PerPmsDept ppd : fs.findAllDept()) {
			if(ppd.getDeptss()==1) {
				list1.add(ppd);
			}
		}
		mod.addAttribute("jnxx", fs.findAllZJnxx());
		mod.addAttribute("Emp",list2);
		mod.addAttribute("Dept",list1);
	}
	
	@RequestMapping("dept_table")
	@ResponseBody
	public Map<String,Object> dept_table(Integer page,Integer rows) {
		Map<String,Object> map = new HashMap<>();
		JSONArray json = new JSONArray();
		map.put("total",fs.findAllDept().size());
		int i=0;
		for(PerPmsDept ppd : fs.findAllDept()){
			if((page-1)*rows<=i&&i<page*rows&&ppd.getPerPmsStms().size()!=0) {
				JSONObject jo = new JSONObject();
				jo.put("deptid", ppd.getDeptid());
	            jo.put("deptname", ppd.getDeptname());
	            jo.put("empsum", ppd.getPerPmsStms().size());
	            jo.put("depttime", ppd.getDepttime());
	            jo.put("deptzy", ppd.getDeptzy());
	            json.add(jo);
			}
            i++;
        }
		map.put("rows",json);
		return map;
	}
}
